package flextv.main.logic {

import flash.events.Event;

import flextv.data.MockPersistenceService;
import flextv.data.PersistenceServiceImpl;
import flextv.data.versions.DataVersionCheckerImpl;
import flextv.main.logic.download.DownloadServiceImpl;
import flextv.main.logic.download.MockDownloadService;
import flextv.search.logic.MockSearchService;
import flextv.search.logic.SearchServiceImpl;
import flextv.search.logic.SearchServices;
import flextv.search.logic.SingleSearchItemServiceImpl;

import org.springextensions.actionscript.context.support.FlexXMLApplicationContext;
	

public class SpringApplicationContext {
	
	private static const CONFIG_FOLER: String = "config/";
	
	/** spring-beans */
	private static const _requiredClasses: Array = [
		SearchServices,
			SearchServiceImpl, MockSearchService,
		MainServices,
			DownloadServiceImpl, MockDownloadService,
			PersistenceServiceImpl, MockPersistenceService, DataVersionCheckerImpl,
			AddItemServiceImpl,
			SingleSearchItemServiceImpl
	];
	
	
	private var appContext: FlexXMLApplicationContext;
	
	
	public function SpringApplicationContext(fnAppContextComplete: Function, contextFiles: Array = null) {
		if(fnAppContextComplete == null) throw new ArgumentError("fnAppContextComplete == null");
		
		appContext = new FlexXMLApplicationContext();
		
		if(contextFiles == null) {
			// default bean definitions
			appContext.addConfigLocation(CONFIG_FOLER + "beans-core.xml");
			appContext.addConfigLocation(CONFIG_FOLER + "beans-search.xml");
			
		} else {
			for each(var contextFile: String in contextFiles) {
				appContext.addConfigLocation(CONFIG_FOLER + contextFile);
			}
		}
		
		appContext.addEventListener(Event.COMPLETE, fnAppContextComplete);
	}
	
	
	public function load(): void {
		appContext.load();
	}

}
}